<?php

namespace api\modules\v1\controllers\marketing;

use Yii;
use api\controllers\OnAuthController;
use common\models\marketing\SourceChannel;
use common\helpers\CommonHelper;

/**
 * Class SourceChannelController
 * @package api\modules\v1\controllers\marketing
 */
class SourceChannelController extends OnAuthController
{
    /**
     * @var SourceChannel
     */
    public $modelClass = SourceChannel::class;

    /**
     * @api {get} /v1/marketing/source-channel/index 来源渠道列表
     * @apiName index
     * @apiGroup SourceChannel
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     */
    public function actionIndexSourceChannel()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $projectId = $this->projectId;

        // 查询结果
        $data = Yii::$app->services->marketingSourceChannel->getList((int)$page, (int)$pageSize, $projectId);

        return $data;
    }

    /**
     * @api {post} /v1/marketing/callback/create 新增来源渠道
     * @apiName create
     * @apiGroup SourceChannel
     *
     * @apiParam {String} name              来源渠道名称
     * @apiParam {Number} status            【1启用、0停用】
     */
    public function actionCreateSourceChannel()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['pid'] = $params['pid'] ?? 0;
        $params['sort'] = $params['sort'] ? $params['sort'] : Yii::$app->services->marketingSourceChannel->getNewSort($params['pid']);
        $params['project_id'] = $this->projectId;

        return Yii::$app->services->marketingSourceChannel->create($params);
    }

    /**
     * @api {post} /v1/marketing/callback/update 编辑来源渠道
     * @apiName update
     * @apiGroup SourceChannel
     *
     * @apiParam {Number} id                来源渠道ID
     * @apiParam {Number} status            状态【0停用 1启用】
     */
    public function actionUpdateSourceChannel()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['project_id'] = $this->projectId;
        return Yii::$app->services->marketingSourceChannel->update($params);
    }


    /**
     * @api {post} /v1/marketing/callback/update-status 编辑来源渠道的状态
     * @apiName update-status
     * @apiGroup SourceChannel
     *
     * @apiParam {Number} id                来源渠道ID
     * @apiParam {Number} status            状态【0停用 1启用】
     */
    public function actionUpdateSourceChannelStatus()
    {
        $id = Yii::$app->request->post('id');
        $status = Yii::$app->request->post('status');

        return Yii::$app->services->marketingSourceChannel->updateStatus($id, $status);
    }

    /**
     * @api {post} /v1/marketing/callback/delete 删除来源渠道
     * @apiName delete
     * @apiGroup SourceChannel
     *
     * @apiParam {Number} id                来源渠道ID
     */
    public function actionDeleteSourceChannel()
    {
        $id = Yii::$app->request->post('id');

        return Yii::$app->services->marketingSourceChannel->delete($id);
    }

    /**
     * @api {post} /v1/marketing/source-channel/true-delete 删除来源渠道
     * @apiName delete
     * @apiGroup SourceChannel
     *
     * @apiParam {Number} id                来源渠道ID
     */
    public function actionTrueDeleteSourceChannel()
    {
        $id = Yii::$app->request->post('id');

        return Yii::$app->services->marketingSourceChannel->trueDelete($id);
    }


    /**
     * @api {get} /marketing/callback/parent-source-channel 父节点
     * @apiGroup 分组名称
     * @apiName SourceChannel
     *
     * @apiDescription 接口描述
     *
     */
    public function actionGetParentSourceChannel()
    {
        $projectId = $this->projectId;
        return Yii::$app->services->marketingSourceChannel->getParentSourceChannel($projectId);
    }

}